home *** CD-ROM | disk | FTP | other *** search
/ FishMarket 1.0 / FishMarket v1.0.iso / fishies / 476-500 / disk_500 / signal / source / signal.c next >
C/C++ Source or Header  |  1992-05-06  |  3KB  |  110 lines

  1. /*
  2.  *  SIGNAL.C    A program to allow processes created by RUN EXECUTE to
  3.  *              signal their parent processes (to indicate that they have
  4.  *              completed their tasks, or are ready to start, etc).
  5.  *
  6.  *              Used in conjunction with WAITFOR.C
  7.  *
  8.  *              Copyright (c) 1989 by Davide P. Cervone, all rights reserved.
  9.  */
  10.  
  11. #include <exec/types.h>
  12. #include <exec/ports.h>
  13. #include <libraries/dos.h>
  14. #include <proto/exec.h>
  15.  
  16. #define MAXCOUNT            10      /* Number of times to try to find port */
  17. #define COUNTWAIT           100     /* Ticks to wait between tries */
  18.  
  19. #define ERROR_NONE           0      /* Normal exit status */
  20. #define ERROR_CANCELLED     10      /* User pressed CTRL-C to cancel */
  21. #define ERROR_BADPORTNAME   11      /* No port name on command line */
  22. #define ERROR_PORTNOTFOUND  12      /* Specified port not found */
  23.  
  24. #define SIGBREAKF_ANY\
  25.    (SIGBREAKF_CTRL_C | SIGBREAKF_CTRL_D | SIGBREAKF_CTRL_E | SIGBREAKF_CTRL_F)
  26.  
  27. /*
  28.  *  DoExit()
  29.  *
  30.  *  General purpose exit routine.  In this case, since there's nothing
  31.  *  special that needs to be cleaned up, just call _exit() with the 
  32.  *  proper return status.  EXECUTE files will report the error codes, 
  33.  *  but interactive users will get no message
  34.  */
  35.  
  36. #define DoExit(status)  _exit(status)
  37.  
  38. extern struct MsgPort *FindPort();
  39. extern long SetSignal();
  40.  
  41.  
  42. /*
  43.  *  CheckForCTRLC()
  44.  *
  45.  *  Checks to see if CTRL-C (or D,E or F) have been pressed.
  46.  */
  47.  
  48. static int CheckForCTRLC()
  49. {
  50.    long theSignals;
  51.    
  52.    theSignals = SetSignal(0L,0L);
  53.    return(theSignals & SIGBREAKF_ANY);
  54. }
  55.  
  56. /*
  57.  *  _main()
  58.  *
  59.  *  Replaces the standard Lattice _main routine (since no IO is performed
  60.  *  we don't nee the usual overhead).  _main expects the command line 
  61.  *  (as entered by the user, including with the program) to be on the
  62.  *  stack as its PortName argument.
  63.  *
  64.  *  First clear the DOS signals, in case any are set from before.
  65.  *  Check that the port name exists, and skip over the command name
  66.  *  in the command line, and any leading blanks.  The port name will
  67.  *  be whatever remains on the command line, including blanks and
  68.  *  special characters.
  69.  *
  70.  *  Next, look for the specified port.  If it is not found, increment
  71.  *  count and wait a few seconds before trying again.  Only try as MAXCOUNT
  72.  *  times, and error exit if the port can't be found in that many tries.
  73.  *
  74.  *  If the port was found, signal the task.  Note that no actual message
  75.  *  is passed.  The message port simply acts as a convenient holding place
  76.  *  for the signal and the name of the port.  The waiting process simply
  77.  *  removes the port when it receives the signal.
  78.  *
  79.  */
  80.  
  81. void _main(PortName)
  82. char *PortName;
  83. {
  84.    struct MsgPort *thePort = NULL;
  85.    int count;
  86.    
  87.    SetSignal(0L,SIGBREAKF_ANY);
  88.  
  89.    if (PortName == NULL) DoExit(ERROR_BADPORTNAME);
  90.    
  91.    while (*PortName != '\0' && *PortName != ' ') PortName++;
  92.    if (*PortName == '\0' || *PortName == '\n') DoExit(ERROR_BADPORTNAME);
  93.    while (*(++PortName) == ' ');
  94.  
  95.    for (count=0; count < MAXCOUNT && thePort == NULL; count++)
  96.    {
  97.       thePort = FindPort(PortName);
  98.       if (thePort == NULL)
  99.       {
  100.          Delay(COUNTWAIT);
  101.          if (CheckForCTRLC()) DoExit(ERROR_CANCELLED);
  102.       }
  103.    }
  104.  
  105.    if (thePort == NULL) DoExit(ERROR_PORTNOTFOUND);
  106.    
  107.    Signal(thePort->mp_SigTask,(1<<thePort->mp_SigBit));
  108.    DoExit(ERROR_NONE);
  109. }
  110.